# -*- coding:utf-8 -*-

# @Time : 2023/5/15 17:27
# @Author : 快乐的小猴子
# @Version : 
# @Function :
import requests
import time
import random
from zhengwuinfos.conf import settings
from zhengwuinfos.conf.proxies import Proxies


class BaseInfos:
    """
    基类
    """
    def get_url(self, num, *args):
        """ 获取完整的url地址 """
        if int(len(args[0])) <= 10:
            url = settings.url_child.format(args[0])
        else:
            url = settings.url.format(args[0], num)
        return url

    def send_reqs(self, url):
        """ 发送请求，获取响应数据 """
        NETWORK_STATUS = True
        try:
            time.sleep(random.randint(2, 5))
            resp = requests.get(url=url, headers=settings.headers, proxies=Proxies().get_proxies())
            # resp = requests.get(url=url, headers=settings.headers).json()
            if resp.status_code == 200:
                return resp.json()
        except:
            # global NETWORK_STATUS
            NETWORK_STATUS = False
            if NETWORK_STATUS == False:
                for i in range(1, 6):
                    print('请求异常, 第 %s 次重复请求' % i)
                    time.sleep(random.randint(1, 3))
                    resp = requests.get(url=url, headers=settings.headers, proxies=Proxies().get_proxies())
                    if resp.status_code == 200:
                        return resp.json()
        return -1

    def get_page_cnts(self, num):
        # 获取单个主题的总页码数
        if num % 10 == 0:
            page_cnts = int(num / 10)
        else:
            page_cnts = int(num / 10) + 1
        return page_cnts
